<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page import="java.util.List,java.util.ArrayList,entity.Message" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>**企业留言板</title>
</head>
<body>
<a href="LogoutServlet">退出</a>
<c:if test="${!empty param.recordsPerPage}">
    <c:set var="recordsPerPage" value="${param.recordsPerPage}" scope="session"></c:set>
</c:if>

<c:set var="temp1" value="${Math.round(sessionScope.numberOfRecords/sessionScope.recordsPerPage)}" scope="page"></c:set>
<c:set var="temp2" value="${sessionScope.numberOfRecords/sessionScope.recordsPerPage}" scope="page"></c:set>

<c:choose>
    <c:when test="${pageScope.temp2>pageScope.temp1}">
        <c:set var="totalPages" value="${pageScope.temp1+1}" scope="session"></c:set>
        <c:set var="currentPage" value="${sessionScope.totalPages}" scope="session"></c:set>
    </c:when>
    <c:otherwise>
        <c:set var="totalPages" value="${pageScope.temp1}" scope="session"></c:set>
        <c:set var="currentPage" value="${sessionScope.totalPages}" scope="session"></c:set>
    </c:otherwise>
</c:choose>

<c:if test="${!empty param.currentPage}">
    <c:set var="currentPage" value="${param.currentPage}" scope="session"></c:set>
</c:if>

<c:if test="${sessionScope.currentPage<1}"><!-- 当前页小于第一页就显示最后一页 -->
<c:set var="currentPage" value="${sessionScope.totalPages}" scope="session"></c:set>
</c:if>

<c:if test="${sessionScope.currentPage>sessionScope.totalPages}"><!-- 当前页大于最后一页就显示第一页 -->
<c:set var="currentPage" value="${1}" scope="session"></c:set>
</c:if>

<c:set var="start" value="${(sessionScope.currentPage-1)*sessionScope.recordsPerPage}" scope="page"></c:set>
<c:set var="end" value="${pageScope.start+sessionScope.recordsPerPage-1}" scope="page"></c:set>

<c:if test="${pageScope.end>sessionScope.numberOfRecords}">
    <c:set var="end" value="${sessionScope.numberOfRecords}" scope="page"></c:set>
</c:if>

<table>
    <tr>
        <td style="text-align:center;" colspan="2"><font size="5">**企业留言板</font></td>
    </tr>
    <c:set var="start" value="1" scope="page" />
    <c:set var="end" value="1" scope="page" />
    <c:forEach items="${sessionScope.mlist}" var="message" begin="${pageScope.start}" end="${pageScope.end}">
        <tr><td colspan="3"  style="text-align:left;width:720px;">${message}</td></tr>
    </c:forEach>
    <tr>
        <td style="text-align:center;">
            <form action="message.jsp">
                <input type="hidden" name="currentPage" value="${sessionScope.currentPage-1}" />
                <input type="submit" value="上一页">
            </form>
        </td><td style="text-align:left;">
        <form action="message.jsp">
            <input type="hidden" name="currentPage" value="${sessionScope.currentPage+1}" />
            <input type="submit" value="下一页">
        </form>
    </td>
    </tr>
    <tr>
        <td style="text-align:right;width:320px;">
            <form action="" method="post">
                共<font size='2' color='blue'>${sessionScope.numberOfRecords}</font>条留言，每页最多显示
                <input type="text" name="recordsPerPage" value="${sessionScope.recordsPerPage}" size=1 />
                <input type="submit" value="确定" />
            </form>
        </td>
        <td style="text-align:left;width:225px;">
            <form action="message.jsp" method="post">
                条留言，当前是第
                <input type="text" name="currentPage" value="${sessionScope.currentPage}" size=1 />
                <input type="submit" value="确定"/>页。
            </form>
        </td>
    </tr>
</table>
<br>
${sessionScope.uName}<font size='3' color='brown'>留言：</font><br>
<form action="<%=request.getContextPath()%>/LeaveMessageServlet" method="post">
    <textarea rows="2" cols="50" name="message"></textarea><font size='2' color='red'>
    ${sessionScope.messageErrorMess}</font>
    <c:remove var="messageErrorMess" scope="session"/>
    <br>
    <input type="submit" value="确定" />  <input type="reset" value="重置" />
</form>
</body>
</html>